package util;

import java.text.ParseException;
import java.util.Date;
import java.util.Scanner;

public class InputUtil {

    private static final Scanner SCANNER=new Scanner(System.in);

    public static int getInputInteger(String tip,int min,int max){
        System.out.println(tip);
        while(true){
            if(SCANNER.hasNextInt()){
                int number=SCANNER.nextInt();
                if(number>=min&&number<= max){
                    return number;
                }else{
                    System.out.printf("输入错误，请输入%d~%d范围内的数字\n",min,max);
                }
            }else{
                System.out.printf("输入错误，请输入%d~%d范围内的数字\n",min,max);
                SCANNER.next();
            }
        }
    }

    public static String getInputText(String tip){
        System.out.println(tip);
        return SCANNER.next();
    }

    public static Date getInputDate(String tip){
        System.out.println(tip);
        while (true) {
            String dateStr=SCANNER.nextLine();
            try {
                return DateUtil.str2Date(dateStr);
            } catch (ParseException e) {
                System.out.println("输入错误，请重新输入，日期格式为yyyy-MM-dd HH:mm:ss");
            }
        }
    }

}
